package com.example.questionservice.controller;

import com.example.questionservice.entity.Question;
import com.example.questionservice.entity.QuestionWrapper;
import com.example.questionservice.entity.Response;
import com.example.questionservice.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    Environment environment;

    @GetMapping("/getAllQuestions")
    public ResponseEntity<List<Question>> getAllQuestions() {
        try {
            return ResponseEntity.ok(questionService.getAllQuestions());
        } catch (Exception e) {
            System.out.println("Error fetching all questions: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }
    }

    @GetMapping("/category/{category}")
    public ResponseEntity<List<Question>> getQuestionsByCategory(@PathVariable String category) {
        try {
            List<Question> questions = questionService.getQuestionsByCategory(category);

            if (questions.isEmpty()) {
                System.out.println("No questions found for category: " + category);
                return ResponseEntity.status(404).body(null);
            }

            return ResponseEntity.ok(questions);
        } catch (Exception e) {
            System.out.println("Error fetching questions by category: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }

    }

    @PostMapping("/addQuestion")
    public ResponseEntity<Question> addQuestion(@RequestBody Question question) {
        try {
            return ResponseEntity.ok(questionService.addQuestion(question));
        } catch (Exception e) {
            System.out.println("Error adding question: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }

    }

    @GetMapping("/generate")
    public ResponseEntity<List<Integer>> generateQuestionsforQuiz(@RequestParam String category, @RequestParam int numOfQuestions) {
        try {
            List<Integer> questions = questionService.generateQuestionsforQuiz(category, numOfQuestions);

            if (questions.isEmpty()) {
                System.out.println("No questions found for category: " + category);
                return ResponseEntity.status(404).body(null);
            }

            return ResponseEntity.ok(questions);
        } catch (Exception e) {
            System.out.println("Error fetching questions by category: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }
    }

    @PostMapping("/getQuestions")
    public ResponseEntity<List<QuestionWrapper>> getQuestionsfromIds(@RequestBody List<Integer> questionIds) {
        System.out.println(environment.getProperty("local.server.port"));
        try {
            List<QuestionWrapper> questions = questionService.getQuestionsByIds(questionIds);

            if (questions.isEmpty()) {
                System.out.println("No questions found for ids: " + questionIds);
                return ResponseEntity.status(404).body(null);
            }

            return ResponseEntity.ok(questions);
        } catch (Exception e) {
            System.out.println("Error fetching questions by ids: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }
    }

    @PostMapping("/getScore")
    public ResponseEntity<Integer> getScore(@RequestBody List<Response> responses) {
        try {
            return ResponseEntity.ok(questionService.getScore(responses));
        } catch (Exception e) {
            System.out.println("Error fetching score: " + e.getMessage());
            return ResponseEntity.status(500).body(null);
        }
    }
}
